package com.ygp.jmm.visible;

import com.ygp.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Visible")
public class Visible4 {
    static  boolean runCondition = true;

    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            log.debug("t 将变量设置为false");
            runCondition = false;
        });
        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (!runCondition) {
                    log.debug("变量发生变化，t2推出循环");
                    break;
                }
            }
        });
        t2.start();
        Sleeper.sleep(1);
        t.start();
        Sleeper.sleep(2);
        log.debug("主线程结束");
    }
}
